package com.luoying.itew.activity;

import android.view.MenuItem;

import androidx.appcompat.app.ActionBar;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.luoying.itew.R;
import com.luoying.itew.adapter.NewInformationAdapter;
import com.luoying.itew.bean.CollectorBean;
import com.luoying.itew.common.AntaiApplication;
import com.luoying.itew.fragment.ChartListener;
import com.luoying.itew.http.HttpRequestUtils;
import com.luoying.itew.http.HttpResponseCallBack;
import com.luoying.itew.util.ThreeDUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by Administrator on 2018/5/24.
 * time 2018-5-10
 * author zm
 */
public class InformationActivity extends BaseActivity {
    private static final String TAG = "InformationActivity";

    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private String[] mTitles;

    //监听集合为了异步加载采集器状态
    //需要合理移除和增加回调否则会导致错误持有Fragment对象，导致内存泄漏
    private ArrayList<ChartListener> listenerList = new ArrayList<>();

    /**
     * 适配器
     */
    NewInformationAdapter adapter;

    /**
     * 当前监测点id
     */
    private int cuurMonitorId;

    //是否有水位采集器
    private boolean isWaterCollector = false;
    //有无测斜仪
    private boolean isInstrumentCollector = false;

    private boolean isYuCollector = false;
    private boolean isZhengCollector = false;

    @Override
    public int getLayoutResId() {
        return R.layout.newinformation_activity;
    }

    @Override
    protected void init() {
        super.init();
        cuurMonitorId = this.getIntent().getIntExtra("cuurMonitorId",0);
        AntaiApplication.cuurMonitorId = cuurMonitorId;
        setViewPager();
        //获取MainActivity数据
        cuurMonitorId = this.getIntent().getIntExtra("cuurMonitorId",0);
    }


    /**
     * 监测点信息接口，3d模型数据通用
     */
    public void getMoitorDetail( ) {
        HttpRequestUtils.getInstance().get3dInfoDetai(cuurMonitorId+"", new HttpResponseCallBack<JsonObject>() {
            @Override
            public void onFail(String message) {
            }

            @Override
            public void onSuccess(JsonObject data) {
                if (data == null) {
                    return;
                }
                try {
                    JSONObject jsonObject = new JSONObject(data.toString());
                    ThreeDUtils.setModel(jsonObject);
                    JSONArray jsonArray = jsonObject.getJSONObject("monitorPoint").getJSONArray("allCollectors");
                    List<CollectorBean> collectBeans = new Gson().fromJson(jsonArray.toString(),new TypeToken<List<CollectorBean>>(){}.getType());
                    for (int i = 0; i < collectBeans.size(); i++) {

                        if(collectBeans.get(i).getCollectorType()==2){
                            isWaterCollector = true;
                        }
                        if(collectBeans.get(i).getCollectorType()==10){
                            isInstrumentCollector = true;
                        }
                        if(collectBeans.get(i).getCollectorType()==15){
                            isYuCollector = true;
                        }
                        if(collectBeans.get(i).getCollectorType()==17){
                            isZhengCollector = true;
                        }
                    }
                    if(listenerList !=null){
                        for (int i = 0; i < listenerList.size(); i++) {
                            if(listenerList.get(i)!=null){
                                listenerList.get(i).onDataUpdate();
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(String message) {
            }
        });
    }


    /**
     * 是否有水位采集器
     * @return
     */
    public boolean isWaterCollector(){
        return  isWaterCollector;
    }

    /**
     * 是否有测斜仪
     * @return
     */
    public boolean isInstrument(){
        return  isInstrumentCollector;
    }

    public boolean isYuCollector() {
        return isYuCollector;
    }

    public boolean isZhengCollector() {
        return isZhengCollector;
    }

    /**
     * 设置viewpager的属性
     */
    private void setViewPager() {
        mTitles = getResources().getStringArray(R.array.new_information);
        adapter = new NewInformationAdapter(getSupportFragmentManager(), mTitles);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(1);
    }

    @Override
    protected void setSystemColor() {
        super.setSystemColor();
        ActionBar actionBar =  getSupportActionBar();
        if(actionBar != null ) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    public int getCuurMonitorId (){
        return cuurMonitorId;
    }

    public void setListenerList(ChartListener listener) {
        if(!listenerList.contains(listener)) {
            listenerList.add(listener);
        }
    }

    public void removeListener(ChartListener listener){
        listenerList.remove(listener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        listenerList.clear();
    }
}
